package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.SimpleTreeListParams;
import com.isunland.managesystem.entity.ZTreeNode;
import com.isunland.managesystem.entity.ZTreeNodeListOriginal;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.Date;

/**
 * 对象功能:  车辆使用申请查询界面
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/7 09:42
 */
public class CarUsedApplyQueryFragment extends BaseFragment {
  protected static final String EXTRA_CONTENT =
      CarUsedApplyQueryActivity.class.getSimpleName() + "EXTRA_CONTENT";

  private final int BEGINREG_DATE = 1;
  private final int ENDREG_DATE = 2;
  private final int ENDREG_SOURCE = 3;
  @BindView(R.id.tv_beginregDate) TextView tvBeginregDate;
  @BindView(R.id.ib_beginregDate) ImageView ibBeginregDate;
  @BindView(R.id.tv_endregDate) TextView tvEndregDate;
  @BindView(R.id.ib_endregDate) ImageView ibEndregDate;
  @BindView(R.id.et_carNumber) EditText etCarNumber;
  @BindView(R.id.ib_carNum) ImageView ibCarNum;

  private String beginDate;
  private String endDate;
  private String carNumber;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.query);
  }

  @Override public void initData() {
    ExtraValueContent queryConten =
        (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    if (queryConten != null) {
      beginDate = queryConten.getExtName1();
      endDate = queryConten.getExtName2();
      carNumber = queryConten.getExtName3();
    } else {
      beginDate = "";
      endDate = "";
      carNumber = "";
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(tvBeginregDate.getText().toString());
        queryConten.setExtName2(tvEndregDate.getText().toString());
        queryConten.setExtName3(etCarNumber.getText().toString());
        Intent intent = new Intent();
        intent.putExtra(EXTRA_CONTENT, queryConten);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override protected int initLayoutId() {
    return R.layout.fragment_car_used_apply_query;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    tvBeginregDate.setText(beginDate);
    tvEndregDate.setText(endDate);
    etCarNumber.setText(carNumber);
  }

  @OnClick({ R.id.ib_beginregDate, R.id.ib_endregDate, R.id.ib_carNum })
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_beginregDate:
        showDialog(BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(beginDate)),
            BEGINREG_DATE);
        break;
      case R.id.ib_endregDate:
        showDialog(BaseYMDTimeDialogFragment.newInstance(MyDateUtil.parse(endDate)), ENDREG_DATE);
        break;
      case R.id.ib_carNum:
        //资源
        volleyGetSource();
        break;
      default:
        break;
    }
  }

  /** 测试项目类别 */
  private void volleyGetSource() {
    SimpleTreeListParams params = new SimpleTreeListParams();
    ZTreeNode item = new ZTreeNode();
    item.setName("资源目录");
    item.setId("0");
    params.setItem(item);
    params.setChildField("id");
    params.setParentField("pid");
    params.setTitleField("name");
    params.setTitle("请选择资源目录");
    params.setClassOriginal(ZTreeNodeListOriginal.class);
    params.setUrl(ApiConst.URL_CAR_APPLY_SOURCE);
    params.setSelectType(SimpleTreeListParams.ONLY_CHILD);//只能选择最底层节点
    params.setParamsNotEmpty(
        new ParamsNotEmpty().put("memberCode", CurrentUser.newInstance(mActivity).getMemberCode())
            .put("dictFlg", "resouceKindCode")
            .put("dataStatus", "publish"));
    BaseVolleyActivity.newInstance(this, ZTreeListActivity.class, params, ENDREG_SOURCE);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == BEGINREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      beginDate = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvBeginregDate.setText(beginDate);
    }
    if (requestCode == ENDREG_DATE) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      endDate = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      tvEndregDate.setText(endDate);
    }
    if (requestCode == ENDREG_SOURCE) {
      //资源目录
      SimpleTreeListParams params = (SimpleTreeListParams) data.getSerializableExtra(EXTRA_PARAMS);
      ZTreeNode item = ((ZTreeNode) params.getItem());
      etCarNumber.setText(item.getName());
    }
  }

  public static CarUsedApplyQueryFragment newInstance(ExtraValueContent queryConten) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, queryConten);
    CarUsedApplyQueryFragment fragment = new CarUsedApplyQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = super.onCreateView(inflater, container, savedInstanceState);
    ButterKnife.bind(this, rootView);
    return rootView;
  }
}

